package com.key.controller.admin.analysisReport;

import com.alibaba.fastjson.JSONObject;
import com.key.common.R;
import com.key.common.dto.AssetsFindDTO;
import com.key.common.dto.EndAssetsFindDTO;
import com.key.common.vo.AssetChangeUpdateVO;
import com.key.common.vo.AssetChangeVO;
import com.key.common.vo.AssetListVO;
import com.key.common.vo.AssetsVO;
import com.key.service.EndAssetsService;
import io.swagger.annotations.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
*
 * @description:到期资产Controller
 * @author: jianjiawei
 * @date: 2023/2/18 14:35
 **/
@Api(value = "到期资产接口API",tags = "到期资产接口API")
@Controller
@RequestMapping("/endAssets")
public class EndAssetsController {
    @Autowired
    EndAssetsService eaService;

    /*
    *
     * @description:显示所有到期资产信息的接口
     * @author: jianjiawei
     * @date: 2023/2/18 15:39
     **/
    @ApiOperation(value = "查询使用到期资产列表",notes = "显示所有到期资产信息",httpMethod = "post", response = AssetListVO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页显示总数",paramType = "query",dataType = "int",required = true)
    })
    @ResponseBody
    @RequestMapping(value = "/findEndAssetsList",method = RequestMethod.POST)
    public R findEndAssetsList(@RequestParam(name = "currentPage") Integer currentPage,@RequestParam(name = "pageSize") Integer pageSize){
        return eaService.findEndAssetsList(currentPage, pageSize);
    }


    /*
    *
     * @description:根据高级搜索查询到期资产接口
     * @author: jianjiawei
     * @date: 2023/2/18 15:45
     **/
    @ApiOperation(value = "根据高级搜索查询到期资产接口",notes = "根据高级搜索查询所有到期资产列表",httpMethod = "post", response = AssetListVO.class)
    @RequestMapping(value = "/findEndAssetsListByAdv",method = RequestMethod.POST)
    @ResponseBody
    public R findEndAssetsListByAdv(@RequestBody @ApiParam(name = "EndAssetsFindDTO",value = "到期资产条件查询",required = true) EndAssetsFindDTO findDTO){

        System.out.println(findDTO);
        R r = eaService.findEndAssetsListByAdv(findDTO);
        System.out.println(r.getData());
        if (r.getData() == "" || r.getData() == null){
            r.setCode(200);
            r.setMsg("没有满足条件的数据");
        }
        return r;
    }

    @ApiOperation(value = "高级搜索回显",notes = "回显高级搜索表单下拉框数据",httpMethod = "post", response = R.class)
    @RequestMapping(value = "/findByAdvShowBack",method = RequestMethod.GET)
    @ResponseBody
    public R showAcAdvSearch(){
        return eaService.findByAdvShowBack();
    }


    @RequestMapping(value = "/exportExcel",method = RequestMethod.GET)
    @ApiOperation(value = "EXCEL导出",notes = "把到期资产信息导出为excel",httpMethod = "get")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=401,message ="用户未授权"),
            @ApiResponse(code=403,message="认证失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HSSFWorkbook workBook = eaService.getWorkBook();
        String userAgent = request.getHeader("User-Agent");

        //excel文件名
        String fileName = "到期资产表"+System.currentTimeMillis()+".xls";
        // 针对IE或者以IE为内核的浏览器：
        if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
            fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
        } else {
            // 非IE浏览器的处理：
            fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
        }
        //响应到客户端
        try {
            OutputStream os = response.getOutputStream();
            response.setContentType("application/x-xls;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            workBook.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /*
    *
     * @description:清理资产
     * @author: jianjiawei
     * @date: 2023/2/25 12:14
     **/
    @ApiOperation(value = "批量清理到期资产的接口",notes = "根据id数组清理到期资产",httpMethod = "post", response = R.class)
    @RequestMapping(value = "/cleanAssets",method = RequestMethod.POST)
    @ResponseBody
    public R cleanAssets(@RequestBody @ApiParam(name = "ids",value = "id数组",required = true) List<Integer> ids){
        return eaService.cleanAssetsByIds(ids);
    }



}
